import os
import sys
import time
from typing import Text
import webbrowser
import resource
import tkinter as tk
import datetime
import threading
import PyQt5.QtWidgets as QtWidgets
from PyQt5.QtCore import Qt, QTimer
import PyQt5.QtGui as QtGui

class Control():
    def GetTime():
        times = (datetime.datetime.strptime("2022-02-04 20:00:00", "%Y-%m-%d %H:%M:%S") - datetime.datetime.now()).days
        timeSecond = (datetime.datetime.strptime("2022-02-04 20:00:00", "%Y-%m-%d %H:%M:%S") - datetime.datetime.now()).seconds
        if times > 0:
            label.setText("<p>距离北京2022冬奥会开幕还有：</p><h1 style='color: red;'><b>{}天</b></h1>".format(str(times)))
            Control.Time.Sleep(20)
        if times == 0:
            m, s = divmod(timeSecond, 60)
            h, m = divmod(m, 60)
            label.setText("<p>距离北京2022冬奥会开幕还有：</p><h1 style='color: red;'><b>{}:{}:{}</b></h1>".format(str(h), str(m), str(s)))
            if h == 0 and m == 0 and s == 0:
                if QtWidgets.QMessageBox.information(widgets, "提示", "北京2022冬奥会现在开幕了，是否现在马上查看开幕式！", QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) == QtWidgets.QMessageBox.Yes:
                    webbrowser.open_new_tab("https://tv.cctv.com/live/index.shtml")
            Control.Time.Sleep(0.5)
        if times < 0 and times >= -17:
            label.setText("<p>距离北京2022已经开幕：</p><h1 style='color: red;'><b>{}天</b></h1>".format(str(-times)))
            Control.Time.Sleep(20)
        if times < -17:
            label.setText("<p>距离北京2022已经闭幕：</p><h1 style='color: red;'><b>{}天</b></h1>".format(str(-times - 17)))
            Control.Time.Sleep(20)
        if not close:
            Control.GetTime()

    def RefreshTime():
        global refresh
        refresh = True

    class Time():
        def Sleep(times):
            global refresh
            for i in range(times * 10):
                if close or refresh:
                    refresh = False
                    break
                widgets.setWindowState(Qt.WindowState.WindowNoState)
                time.sleep(0.1)

    def Exit():
        global close
        close = True
        #os._exit()
        sys.exit()

    class About():
        def AboutQt():
            QtWidgets.QMessageBox.aboutQt(widgets, "关于 Qt")

        def AboutProgram():
            QtWidgets.QMessageBox.about(widgets, "关于这个程序", "<p>一个北京2022冬奥会的倒计时工具</p>")

close = False
refresh = False

screen = tk.Tk()
screen.withdraw()
screenWidth = screen.winfo_width()
screenHeight = screen.winfo_height()
apps = QtWidgets.QApplication(sys.argv)
apps.setQuitOnLastWindowClosed(False)
widgets = QtWidgets.QWidget()

widgets.setAttribute(Qt.WA_TranslucentBackground)
label = QtWidgets.QLabel(widgets, text="<p>距离北京2022冬奥会开幕还有：</p><h1 style='color: red;'><b>0天</b></h1>")
label.setStyleSheet('''font: 25px;''')
#widgets.setGeometry(800, 600, 0, 0)
widgets.setFixedSize(400, 150)
widgets.setWindowFlags(Qt.WindowStaysOnBottomHint|Qt.FramelessWindowHint|Qt.Tool)
widgets.move(widgets.screen().virtualSize().width() - 400, widgets.screen().virtualSize().height() - 150)
widgets.setWindowIcon(QtGui.QIcon(":/Icon.svg"))

menu = QtWidgets.QMenu()
refreshTime = QtWidgets.QAction(text="立即刷新", triggered=Control.RefreshTime)
aboutThisProgram = QtWidgets.QAction(text="关于这个程序", triggered=Control.About.AboutProgram)
aboutQt = QtWidgets.QAction(text="关于Qt", triggered=Control.About.AboutQt)
exit = QtWidgets.QAction(text="退出程序", triggered=Control.Exit)

menu.addAction(refreshTime)
menu.addSeparator()
menu.addAction(aboutThisProgram)
menu.addAction(aboutQt)
menu.addSeparator()
menu.addAction(exit)
icon = QtWidgets.QSystemTrayIcon()
icon.setToolTip("北京2022冬奥会开幕式倒计时")
icon.setIcon(QtGui.QIcon(":/Icon.svg"))
icon.setContextMenu(menu)
icon.show()
threading.Thread(target=Control.GetTime).start()
widgets.show()
sys.exit(apps.exec_())
